class Solution
{
public:
    bool isValidBST(TreeNode* root)
    {
        return isValidBST(root, LONG_MIN, LONG_MAX);
    }

private:
    bool isValidBST(TreeNode* node, long minVal, long maxVal)
    {
        if (node == nullptr) return true;

        if (node->val <= minVal || node->val >= maxVal) return false;

        return isValidBST(node->left, minVal, (long)node->val) &&
            isValidBST(node->right, (long)node->val, maxVal);
    }
};
